package dk.ucn.houseenabler;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class PersonalComponent extends Activity {

	private TextView headerText;
	private ListView list;
	private Button save_person_data_button;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_component);
        save_person_data_button = (Button)findViewById(R.id.button_save_person_data);
        headerText = (TextView)findViewById(R.id.textView_personalComponentHeader);
        list = (ListView)findViewById(R.id.listViewPerson);
        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);        
        String[] pq = getResources().getStringArray(R.array.personal_questions);
        
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getParent().getApplicationContext(),android.R.layout.simple_list_item_multiple_choice, pq);
        list.setAdapter(adapter);
        list.setLongClickable(true);
        list.setOnItemLongClickListener (new OnItemLongClickListener() {
        	  public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
        		  		String text = ((TextView) view).getText().toString();
						Toast.makeText(getParent().getApplicationContext(),text , Toast.LENGTH_LONG).show();
						return true;
        		  }
        		});
        
       
        	save_person_data_button.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				//savedatahere
				byte[] answers = null;
				answers = new byte[14];
				int index = 0;			
				
				while(index < answers.length)
				{
				if(list.isItemChecked(index))
				{
					answers[index] = 1;
					System.out.println("Index: " +index + " isItemChecked: " + list.isItemChecked(index));
				}else 
				{
					answers[index] = 0;
					System.out.println("Index: " +index + " isItemChecked: " + list.isItemChecked(index));
				}
				index++;
				}
				
				byte type = 0;
				
				try {
					FileHandler.exportToFile(MainActivity.getSubjectName(), answers, type);
					MainActivity.changeTab(2);
				} catch (Exception ex) {
					// TODO Auto-generated catch block
					Toast.makeText(getParent().getApplicationContext(), "An error has occured, check that a name has been saved!", Toast.LENGTH_LONG).show();
					ex.printStackTrace();
				}
				
				
			}
		});
        
	}
    @Override
    public void onResume() {
    	super.onResume();
        if(MainActivity.getSubjectName()!=null)
        {
        headerText.setText(getResources().getString(R.string.txtview_personalcomponent_header) + MainActivity.getSubjectName());
        }else{
        	headerText.setText("");
        }

    	
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //getMenuInflater().inflate(R.menu.activity_personal_component, menu);
        return true;
    }
}
